import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/RootView.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/root',
            name: 'root',
            component: HomeView,
            children: [
                {
                    path: '/demo/formalTable',
                    name: 'formalTable',
                    component: () => import('../views/demo/FormalTable.vue')
                },
                {
                    path: '/mem',
                    name: 'sxlnMember',
                    component: () => import('../views/sxln/member/Entrance.vue')
                },
                // {
                //     path: '/sys/dict',
                //     name: 'sysDict',
                //     component: () => import('../views/sys/dict/Entrance.vue')
                // },
                // {
                //     path: '/sys/role',
                //     name: 'sysRole',
                //     component: () => import('../views/sys/role/Entrance.vue')
                // },
                // {
                //     path: '/sys/menu',
                //     name: 'sysMenu',
                //     component: () => import('../views/sys/menu/Entrance.vue')
                // },
                // {
                //     path: '/sys/user',
                //     name: 'sysUser',
                //     component: () => import('../views/sys/user/Entrance.vue')
                // },
                // {
                //     path: '/sys/test',
                //     name: 'test',
                //     component: () => import('../views/sys/test/TestMain.vue')
                // },
                {
                    path: '/sys/dict/option/:dictId',
                    name: 'sysDictOption',
                    component: () => import('../views/sys/dict/DictOptionTable.vue')
                },
                {
                    path: '/',
                    name: 'about',
                    // route level code-splitting
                    // this generates a separate chunk (About.[hash].js) for this route
                    // which is lazy-loaded when the route is visited.
                    component: () => import('../views/AboutView.vue')
                }
            ]
        },
        {
            name: 'login',
            path: '/login',
            component: () => import('@/views/auth/Login.vue')
        },
        {
            path: '/:pathMatch(.*)',
            name: '404',
            component: () => import('../views/404.vue')
        }

    ]
})

import {useMenuStates} from "@/stores/Menu";
// 从menu.json获取父子菜单id
// import menuJson from '@/layout/menu/menu.json'
import {TOKEN_KEY} from "@/core/utils/request";
import {message} from "ant-design-vue";
import {useGlobalMenuStore, useGlobalRouteAndPermStore} from "@/stores/Core";

const useRouter = () => {
    router.beforeEach(async (to, from, next) => {
        const path = to.path
        if (path == '/login') {
            next()
        } else {
            const token = localStorage.getItem(TOKEN_KEY)
            if (!token) {
                message.error('请先登录')
                setTimeout(() => {
                    next('/login')
                }, 500)
                return
            }
            const {routes, initBuildRoute} = useGlobalRouteAndPermStore()
            if (routes.length === 0) {
                await initBuildRoute(router)
                // pathGetMenu(path)
                next(path)
            } else {
                next()
            }
            // next('/404')
        }
    })

    return router
}


export default useRouter